package com.siyoumi.component.http;

import com.siyoumi.controller.AdminApiController;
import com.siyoumi.util.XDate;
import com.siyoumi.util.XReturn;
import com.siyoumi.util.XStr;
import com.siyoumi.validator.XValidator;
import lombok.Getter;
import lombok.NonNull;

import java.net.URI;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class InputData
        extends HashMap<String, Object>
        implements InputDataInterface {
    /**
     * 搜索字段，用于检查是否使用搜索条件
     */
    @Getter
    List<String> searchKeys = new ArrayList<>();

    /**
     * 添加搜索字段
     *
     * @param keys
     */
    public void addSearchKey(List<String> keys) {
        getSearchKeys().addAll(keys);
    }

    public void addSearchKey(String... key) {
        addSearchKey(Arrays.asList(key));
    }

    /**
     * 使用指定的搜索条件
     */
    public Boolean doSearch() {
        return getSearchKeys().stream().anyMatch(k -> XStr.hasAnyText(input(k)));
    }

    public Boolean doNotSearch() {
        return !doSearch();
    }


    public void dateSearch(AdminApiController admin, String keyA, String keyB) {
        dateSearch(admin, keyA, keyB, 365, true, 30);
    }

    /**
     * 检查时间，例如：366天内
     *
     * @param keyA         开始日期key
     * @param keyB         结束日期key
     * @param maxDays      最大搜索天数
     * @param defSearchDay 默认搜索天数区间
     * @param setDef       是否设置默认值
     */
    public void dateSearch(AdminApiController admin, String keyA, String keyB, int maxDays, Boolean setDef, int defSearchDay) {
        String dateA = input(keyA);
        String dateB = input(keyB);

        if (setDef) {
            //设置默认值
            if (XStr.isNullOrEmpty(dateA)
                    || XStr.isNullOrEmpty(dateB)) {
                //没有输入内容只显示当天
                dateA = XDate.toDateTimeString(XDate.today().minusDays(defSearchDay));
                dateB = XDate.toDateTimeString(XDate.today().plusDays(1).minusSeconds(1));
            }
            admin.setPageFrom(keyA, dateA);
            admin.setPageFrom(keyB, dateB);
            put(keyA, dateA);
            put(keyB, dateB);
        }

        //判断日期是否正确
        if (XStr.isNullOrEmpty(dateA)
                || XStr.isNullOrEmpty(dateB)) {
            //pass
        } else {
            LocalDateTime a = XDate.parse(dateA);
            LocalDateTime b = XDate.parse(dateB);

            Duration between = XDate.between(a, b);
            if (between.toDays() > maxDays) {
                XValidator.err(10000, XStr.concat("日期：" + maxDays, "最多天"));
            }
        }
    }

    static public InputData getIns() {
        return new InputData();
    }

    /**
     * a=1&b=2 转 inputData
     *
     * @param url
     */
    static public InputData parse(String url) {
        URI uri = URI.create(url);
        String query = uri.getQuery();
        if (XStr.isNullOrEmpty(query)) {
            return getIns();
        }

        InputData inputData = getIns();
        String[] queryArr = query.split("&");
        for (String q : queryArr) {
            String[] kv = q.split("=");
            inputData.put(kv[0], kv[1]);
        }
        return inputData;
    }

    static public InputData fromRequest() {
        return XHttpContext.InputAll();
    }

    @Override
    public String input(@NonNull String k, String def) {
        String v = (String) get(k);
        if (XStr.isNullOrEmpty(v)) {
            return def;
        }
        return v.trim();
    }

    public Boolean existsKey(String key) {
        return containsKey(key);
    }
}
